package code.tools.commponents
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.structure.manager.font.FontManager;
	import code.structure.manager.language.LanguageManager;
	import code.untils.FiltersEnum;
	import code.untils.UiNameConst;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	/**
	 *树形菜单 
	 * @author liangqian
	 * 
	 */	
	public class Tree extends Component
	{
		public static const TREE_STYLE_GREEN:int = 10;
		public static const TREE_STYLE_BLUE_WITH_ICON:int = 20;
		
		private static const titleFilters:DropShadowFilter = new DropShadowFilter(1,90,0xffffff,1,3,3,1.48);
		
		private static const NORMAL_HEIGHT:int = 31;
		public static const TITLE_SELECTED_COLOR:uint = 0x807C0C;
		
		public static const TITLE_NORMAL_COLOR_1:uint = 0x267108;
		public static const TITLE_NORMAL_COLOR_2:uint = 0xFF7E00;
		public static const TITLE_NORMAL_COLOR_3:uint = 0x39687E;
		
		public static const TITLE_NORMAL_FILTERS_1:Array = [titleFilters];
		public static const TITLE_NORMAL_FILTERS_2:Array = [FiltersEnum.ALERT_TITLE_FILTER,FiltersEnum.CHARTS_PRIZE_DROPSHADOWFILTER];
		/**
		 *内容区域
		 */
		private var content:Sprite;
		private var _minimized:Boolean;
		/**
		 *内容与内容之间的间隔
		 */
		private var _spacing:Number = 2;
		/**
		 *标题区域 
		 */		
		private var titleMc:MovieClip;
		private var titleTf:TextField;
		
		/** 脚注Icon  */
		private var _footnodeIcon:Sprite;
		
		private var _title:String;
		private var _hoverTitle:Boolean;
		private var _titleIconStr:String;
		private var titleIcon:Sprite;
		private var _titleClick:Function;
		private var _contentH:int;
		private var contentBg:Sprite;
		
		private var _titleNormalColor:uint;
		private var _titleSelectedColor:uint;
		private var _titleFilters:Array;
		private var _style:int;
		/**
		 * 
		 * @param parent
		 * @param xpos
		 * @param ypos
		 * @param titleStr 标题名
		 * @param titleClick 标题区域点击事件
		 * @param contentH 若为-1，将按照内容多少来算高度，若不为-1，则根据contentH固定内容的高度
		 * @param titleNormalColor 标题没选中时的颜色，见本类枚举
		 * @param titleSelectedColor 标题选中时的颜色 ，见本类枚举
		 * @param filtersStyle 文字滤镜，0表示TITLE_NORMAL_FILTERS_1，其他值表示TITLE_NORMAL_FILTERS_2
		 * 
		 */
		public function Tree(parent:DisplayObjectContainer, xpos:Number, ypos:Number,titleStr:String,titleClick:Function,contentH:int = -1,
		titleNormalColor:uint = 0x267108,titleSelectedColor:uint = 0x807C0C,filtersStyle:int = 0,style:int = 10)
		{
			_titleNormalColor = titleNormalColor;
			_titleSelectedColor = titleSelectedColor;
			_titleFilters = filtersStyle == 0 ? TITLE_NORMAL_FILTERS_1 : TITLE_NORMAL_FILTERS_2;
			_style = style;
			_titleClick = titleClick;
			_contentH = contentH;
			super(parent, xpos, ypos);
			title = titleStr;
//			titleIconStr = titleIconKey;
		}
		
		override protected function init():void
		{
			super.init();
			setSize(162 , 170);
		}
		
		override protected function addChildren():void
		{
			contentBg = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.PANEL_VIEW,'tree_content_bg')as Class)as Sprite;
			super.addChild(contentBg);
			contentBg.x = 1;
			contentBg.y = 13;
			_width = contentBg.width;
			/* 内容区域 */
			content = new Sprite();
			content.mouseEnabled = false;
			content.y = 34;
//			content.visible = !minimized;
			content.addEventListener(Event.RESIZE, onResize);
			
			super.addChildren();
			
			/* 标题区域  */
			switch(_style)
			{
				case TREE_STYLE_GREEN:
					titleMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.PANEL_VIEW,'treeTitleView')as Class)as MovieClip;
					titleTf = Helpers.FormatTextField(titleMc,11,2,145,TextFieldAutoSize.LEFT,_titleNormalColor,FontManager.getInstance().embeddedFontBName,18);
					(titleTf as AutoTextField).openZoom(145,26);
					break;
				case TREE_STYLE_BLUE_WITH_ICON:
					titleMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.PANEL_VIEW,'treeTitleView2')as Class)as MovieClip;
					titleTf = Helpers.FormatTextField(titleMc,36,2,118,TextFieldAutoSize.LEFT,_titleNormalColor,FontManager.getInstance().embeddedFontBName,18);
					(titleTf as AutoTextField).openZoom(118,26);
					break;
			}
	
			titleTf.mouseEnabled = false;
			titleTf.filters = _titleFilters;
			titleMc.buttonMode = true;
			titleMc.addEventListener(MouseEvent.CLICK , onMinimize);
			titleMc.addEventListener(MouseEvent.ROLL_OVER , titleHoverHandler);
			
			/** 脚注Icon  */
			_footnodeIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.PANEL_VIEW,'footnodeIcon')as Class)as Sprite;
			Helpers.processTf(_footnodeIcon,'new_text',LanguageManager.getLanguageInfo('TITLE_NEW'),FontManager.getInstance().embeddedFontCName,true,true);
			_footnodeIcon.x = _width - _footnodeIcon.width - 8;
			_footnodeIcon.y = (titleMc.height - _footnodeIcon.height)*.5;
			_footnodeIcon.visible = false;
			titleMc.addChild(_footnodeIcon);
			
			super.addChild(titleMc);
			minimized = true;
		}
	
		override public function addChild(child:DisplayObject) : DisplayObject
		{
			content.addChild(child);
			invalidate();
			return child;
		}
		override public function removeChild(child:DisplayObject) : DisplayObject
		{
			content.removeChild(child);
			invalidate();
			return child;
		}
		
		override public function get height():Number
		{
			if (contains(content))
			{
				if(content.numChildren > 0)
					return NORMAL_HEIGHT + (_contentH == -1 ? content.height : _contentH) + 10;
				return NORMAL_HEIGHT;
			}
			else
			{
				return NORMAL_HEIGHT;
			}
		}
		
		override public function get numChildren():int
		{
			return content.numChildren;
		}

		override public function getChildAt(index:int):DisplayObject
		{
			return content == null || content.numChildren < index ? null : content.getChildAt(index);
		}
		
		override public function draw() : void
		{
			_width = 0;
//			_height = NORMAL_HEIGHT;
			var ypos:Number = 0;
			var i:int
			const len:int = content.numChildren;
			for(; i < len; i++)
			{
				var c:DisplayObject = content.getChildAt(i);
				if(!c.visible) continue;
				c.y = ypos;
				ypos += c.height;
				ypos += _spacing;
//				_width = content.x + Math.max(_width, c.width);
			}
//			if(_contentH == -1)
//				_height = NORMAL_HEIGHT + ypos - _spacing;
//			else
//				_height = NORMAL_HEIGHT + _contentH;
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		public function removeAll():void
		{
			while(content.numChildren)
				content.removeChildAt(0);
		}
		
		private function onMinimize(e:MouseEvent):void
		{
			if(minimized == false)return;
			minimized = !minimized;
			if(_titleClick != null)
				_titleClick(this);
		}
		
		private function titleHoverHandler(e:MouseEvent):void
		{
			hoverTitle = true;
			titleMc.addEventListener(MouseEvent.ROLL_OUT,titleOutHandler);
		}
		
		private function titleOutHandler(e:MouseEvent):void
		{
			hoverTitle = false;
			titleMc.removeEventListener(MouseEvent.ROLL_OUT,titleOutHandler);
		}
		
		/**
		 *内容条目间隔 
		 * @param s
		 * 
		 */		
		public function set spacing(s:Number):void
		{
			_spacing = s;
			invalidate();
		}
		public function get spacing():Number
		{
			return _spacing;
		}
		
		/**
		 *标题文字 
		 * @param t
		 * 
		 */		
		public function set title(t:String):void
		{
			if(_title == t) return;
			_title = t;
			titleTf.text = t;
			titleTf.textColor = !minimized ? _titleSelectedColor : _titleNormalColor;
		}
		
		public function get title():String
		{
			return _title;
		}

		/**
		 *展开/收缩 ,为true，表示收缩，为false，表示展开
		 * @param value
		 * 
		 */		
		public function set minimized(value:Boolean):void
		{
			_minimized = value;
			if(_minimized)
			{
				if(contains(content)) super.removeChild(content);
				_height = NORMAL_HEIGHT;
				titleMc.gotoAndStop(1);
				titleTf.textColor = _titleNormalColor;
				contentBg.height = 0;
				titleMc.mouseEnabled = true;
				titleMc.filters = [FiltersEnum.TREE_NORMAL_DROPSHADOWFILTER];
			}
			else
			{
				titleMc.mouseEnabled = false;
				if(!contains(content)) super.addChildAt(content,1);
				if(content.numChildren == 0)
					contentBg.height = 0;
				else
					contentBg.height = NORMAL_HEIGHT + (_contentH == -1 ? content.height : _contentH) - 5;
				_height = contentBg.y + contentBg.height;
				titleMc.gotoAndStop(3);
				titleTf.textColor = _titleSelectedColor;
				titleMc.filters = null;
			}
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		public function get minimized():Boolean
		{
			return _minimized;
		}
	
		private function onResize(event:Event):void
		{
			invalidate();
		}

		/**
		 *鼠标是否移入标题了  
		 * @return 
		 * 
		 */		
		public function get hoverTitle():Boolean
		{
			return _hoverTitle;
		}

		public function set hoverTitle(value:Boolean):void
		{
			_hoverTitle = value;
			if(value)
			{
//				titleTf.textColor = 0x2984C3;
				titleMc.gotoAndStop(2);
			}
			else
			{
				if(!minimized)
				{
					titleTf.textColor = _titleSelectedColor;
					titleMc.gotoAndStop(3);
				}
				else
				{
					titleTf.textColor = _titleNormalColor;
					titleMc.gotoAndStop(1);
				}
			}
			titleMc.addChild(titleTf);
		}
		
		/**
		 *设置是否显示脚注icon 
		 * @param value
		 * 
		 */
		public function set isShowFootnodeIcon(value:int):void
		{
			_footnodeIcon.visible = value > 0;
		}
		
		/**
		 *设置是否显示脚注icon 
		 * @param value
		 * 
		 */
		public function get footnodeIconVisible():Boolean
		{
			return _footnodeIcon.visible;
		}

//		/**
//		 *加载title icon 
//		 * @return 
//		 * 
//		 */		
//		public function get titleIconStr():String
//		{
//			return _titleIconStr;
//		}
//
//		public function set titleIconStr(value:String):void
//		{
//			_titleIconStr = value;
//			if(titleIcon && titleMc.contains(titleIcon))
//			{
//				titleMc.removeChild(titleIcon);
//				titleIcon = null;
//			}
//			titleIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.PANEL_VIEW,value)as Class)as Sprite;
//			titleIcon.x = (46 - titleIcon.width)*.5;
//			titleIcon.y = (titleMc.height - titleIcon.height)*.5;
//			titleMc.addChild(titleIcon);
//		}
	}
}